package com.itheima.reggie.util;

import com.itheima.reggie.entity.Weather;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;

public class WeatherDown {


    public static ArrayList<Weather> weather(String path) throws Exception{
        //String path = "202202";
        String url = "https://m.tianqi.com/lishi/hefei/"+path+".html";
        Document document = Jsoup.parse(new URL(url), 10000);
        Elements alioq = document.getElementsByClass("listto");
        String text = alioq.text();
        //System.out.println(text);

        ArrayList<Weather> list = new ArrayList<>();
        String[] weathers = text.split("级");
        for (String weather : weathers) {
            String trim = weather.trim();
            //System.out.println(trim);
            String[] s = trim.split(" ");

            String date = s[0].substring(0, 10);

            String weekday = s[0].substring(10);
            String high = s[1].substring(0, s[1].length()-1);
            String low = s[2].substring(0, s[2].length());
            String wind = s[4].substring(s[4].length() - 1);
            Weather w = new Weather();
            w.setDate(date);
            w.setWeekday(weekday);
            w.setHighTemperature(high);
            w.setLowTemperature(low);
            w.setWeather(s[3]);
            w.setWind(wind);
            list.add(w);
        }
        return list;
    }


}
